package com.shoppingmall.controller;

import com.shoppingmall.commons.OrderParam;
import com.shoppingmall.commons.Result;
import com.shoppingmall.model.OrderDesc;
import com.shoppingmall.model.Orders;
import com.shoppingmall.model.view.OrderDescView;
import com.shoppingmall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.ls.LSOutput;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {


    @Autowired
    private OrderService orderService;
    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody OrderParam orderParam){
        Orders orders=orderParam.getOrder();
        Integer goods_ids[]=orderParam.getGoods_ids();
        List<OrderDesc> orderDescList=new ArrayList<>();
        for(int i=0;i<goods_ids.length;i++){
            OrderDesc orderDesc=new OrderDesc();
            orderDesc.setGoods_id(goods_ids[i]);
            orderDescList.add(orderDesc);
        }
        orderService.addOrder(orders,orderDescList);
        return Result.success("订单添加成功！");

    }
    @GetMapping("/getOrders")
    public List<Orders>getOrders(int user_id){
        return orderService.getOrders(user_id);
    }
    @GetMapping("/getOrderDesc")
    public List<OrderDescView> getOrderDesc(String order_id){
        return orderService.getOrderDesc(order_id);
    }
    @GetMapping("/getOrderById")
    public List<Orders> getOrderById(String order_to){ return orderService.getOrderById(order_to);}
}
